package io.renren.controller;

import io.renren.common.utils.R;
import io.renren.config.HttpClientUtils;

import io.renren.entity.*;
import io.renren.service.*;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;


/**
 * 我的界面
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/api")
public class MyMineController {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Autowired
	UsersService usersService;

	@Autowired
	BannerService bannerService;

	@Autowired
	CourseService courseService;

	@Autowired
	CourseuserService courseuserService;

	@Autowired
	ClasstimeService classtimeService;

	@Autowired
	IndexbannerService indexbannerService;

	@Autowired
	CoursedescriptService coursedescriptService;

	@Autowired
	UsehelpService usehelpService;

	@Autowired
	CourseteacherService courseteacherService;

	@Autowired
	QuestionService questionService;

	@Autowired
	ClasstimeuserService classtimeuserService;

	@Autowired
	ClasstimeteacherService classtimeteacherService;



	/**
	 * 用户登陆
	 * @param code
	 * @param headimgurl 用户头像
	 * @param nickname 用户昵称
	 * @param gender 性别
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public R login(@RequestParam("code")String code, @RequestParam("headimgurl")String headimgurl, @RequestParam("nickname")String nickname,
                   @RequestParam("gender")String gender) {
		if(code ==null){
			return R.error("code不能为空");
		}else if(headimgurl ==null){
			return R.error("用户头像不能为空");
		}else if(nickname ==null){
			return R.error("用户昵称不能为空");
		}else{

			String urlopenid = "https://api.weixin.qq.com/sns/jscode2session?appid=wx76f63d5c5ac1ae0d&secret=270fbcfd1a03a05dad590943cbcf011e&js_code="+code+"&grant_type=authorization_code";
			JSONObject json = HttpClientUtils.httpRequest(urlopenid,"GET",null);
			String openid = json.getString("openid");
			UsersVOEntity users = usersService.getUserInfoByOpenid(openid);

			if(users!=null){
				users.setLastlogintime(new Date());
				users.setHeadimg(headimgurl);
				users.setUsername(nickname);
				users.setGender(gender);
				users.setLastlogintime(new Date());
				usersService.updateById(users);
			}else{
				users = new UsersVOEntity();
				users.setCreatetime(new Date());
				users.setLastlogintime(new Date());
				users.setOpenid(openid);
				users.setHeadimg(headimgurl);
				users.setUsername(nickname);
				users.setGender(gender);
				users.setRole(0);
				usersService.save(users);
			}
			return R.ok().put("userid",users.getId()).put("openid",openid).put("role",users.getRole());
		}

	}

    /**
     * 用户信息
     * @param userid
     * @return
     */
    @RequestMapping("/user")
    @ResponseBody
    public R user(@RequestParam("userid")Integer userid) {
        if(userid ==null){
            return R.error("用户id不能为空");
        }else{
            UsersVOEntity user = usersService.getById(userid);
            if(user!=null){
                return R.ok().put("headimg",user.getHeadimg())
                        .put("username",user.getUsername()).put("id",user.getId())
                        .put("telphone",user.getTelphone());
            }else{
                return R.ok("找不到该用户");
            }

        }

    }

	/**
	 * 轮播图
	 * @return
	 */
	@RequestMapping("/banner")
	@ResponseBody
	public R banner() {
		List<BannerEntity> list = bannerService.getBanner();
		if(list!=null&&list.size()>0){
			JSONArray array = new JSONArray();
			for(BannerEntity entity:list){
				JSONObject json = new JSONObject();
				json.put("id",entity.getId());
				json.put("imgurl",entity.getImgurl());
				json.put("type",entity.getType());
				json.put("url",entity.getHrefurl());
				array.add(json);

			}
			return R.ok().put("data",array);
		}else{
			return R.ok("没有数据");
		}

	}

	/**
	 * 首页轮播图
	 * @return
	 */
	@RequestMapping("/indexbanner")
	@ResponseBody
	public R indexbanner() {
		List<IndexbannerEntity> list = indexbannerService.getIndexBanner();
		if(list!=null&&list.size()>0){
			JSONArray array = new JSONArray();
			for(IndexbannerEntity entity:list){
				JSONObject json = new JSONObject();
				json.put("id",entity.getId());
				json.put("imgurl",entity.getImgurl());
				json.put("type",entity.getType());
				json.put("url",entity.getHrefurl());
				array.add(json);

			}
			return R.ok().put("data",array);
		}else{
			return R.ok("没有数据");
		}

	}

	/**
	 * 课程
	 * @return
	 */
	@RequestMapping("/course")
	@ResponseBody
	public R course() {
		List<CourseEntity> list = courseService.getCourse();
		if(list!=null&&list.size()>0){
			JSONArray array = new JSONArray();
			for(CourseEntity entity:list){
				JSONObject json = new JSONObject();
				json.put("id",entity.getId());
				json.put("imgurl",entity.getImg());
				json.put("title",entity.getTitle());
				json.put("time",sdf.format(entity.getClasstime()));
				array.add(json);

			}
			return R.ok().put("data",array);
		}else{
			return R.ok("没有数据");
		}

	}

	/**
	 * 我的课程首页
	 * @param userid 用户id
	 * @return
	 */
	@RequestMapping("/mycourse")
	@ResponseBody
	public R mycourse(@RequestParam("userid")Integer userid) {
		if(userid == null){
			return R.error("用户id不能为空");
		}else{
			UsersVOEntity user = usersService.getById(userid);
			if(user!=null){
				if(user.getRole()==0) {//学生
					List<CourseuserEntity> list = courseuserService.getMyCourse(userid);
					if(list!=null&&list.size()>0){
						JSONArray array = new JSONArray();
						for(CourseuserEntity entity:list){
							JSONObject json = new JSONObject();
							CourseEntity courseEntity = courseService.getById(entity.getCourseid());
							if(courseEntity!=null){
								json.put("id",courseEntity.getId());
								json.put("imgurl",courseEntity.getImg());
							}
							array.add(json);

						}
						return R.ok().put("data",array);
					}else{
						return R.ok("没有数据");
					}
				}else{//老师
					List<CourseteacherEntity> list = courseteacherService.getCourseTeacher(userid);
					if(list!=null&&list.size()>0){
						JSONArray array = new JSONArray();
						for(CourseteacherEntity entity:list){
							JSONObject json = new JSONObject();
							CourseEntity courseEntity = courseService.getById(entity.getCourseid());
							if(courseEntity!=null){
								json.put("id",courseEntity.getId());
								json.put("imgurl",courseEntity.getImg());
							}
							array.add(json);

						}
						return R.ok().put("data",array);
					}else{
						return R.ok("没有数据");
					}
				}
			}else{
				return R.ok("没有该用户信息");
			}


		}


	}

	/**
	 * 我的课程列表
	 * @param userid 用户id
	 * @return
	 */
	@RequestMapping("/mycourseinfo")
	@ResponseBody
	public R mycourseinfo(@RequestParam("userid")Integer userid) {
		if(userid == null){
			return R.error("用户id不能为空");
		}else{
			UsersVOEntity user = usersService.getById(userid);
			if(user.getRole()==0){//学生
				List<CourseuserEntity> list = courseuserService.getCourseUser(userid);
				if(list!=null&&list.size()>0){
					JSONArray array = new JSONArray();
					for(CourseuserEntity entity:list){
						JSONObject json = new JSONObject();
						CourseEntity courseEntity = courseService.getById(entity.getCourseid());
						if(courseEntity!=null){
							json.put("id",courseEntity.getId());
							json.put("imgurl",courseEntity.getImg());
							json.put("title",courseEntity.getTitle());
							json.put("time",sdf.format(courseEntity.getClasstime()));
							String statu = "报名中";
							if(entity.getStatu()==1){
								statu = "已报名";
							}
							if(entity.getStatu()==2){
								statu = "已结束";
							}
							json.put("statu",statu);
						}
						array.add(json);

					}
					return R.ok().put("data",array);
				}else{
					return R.ok("没有数据");
				}
			}else{//老师
				List<CourseteacherEntity> list = courseteacherService.getCourseTeacher(userid);
				if(list!=null&&list.size()>0){
					JSONArray array = new JSONArray();
					for(CourseteacherEntity entity:list){
						JSONObject json = new JSONObject();
						CourseEntity courseEntity = courseService.getById(entity.getCourseid());
						if(courseEntity!=null){
							json.put("id",courseEntity.getId());
							json.put("imgurl",courseEntity.getImg());
							json.put("title",courseEntity.getTitle());
							json.put("time",sdf.format(courseEntity.getClasstime()));

						}
						array.add(json);

					}
					return R.ok().put("data",array);
				}else{
					return R.ok("没有数据");
				}
			}

		}


	}

	/**
	 * 课程详情
	 * @courseid 课程id
	 * @return
	 */
	@RequestMapping("/courseDetail")
	@ResponseBody
	public R courseDetail(@RequestParam("courseid")Integer courseid) {
		if(courseid == null){
			return R.error("课程id不能为空");
		}else{
			CourseEntity courseEntity = courseService.getById(courseid);
			if(courseEntity!=null){
				JSONObject json = new JSONObject();
				json.put("id",courseEntity.getId());
				json.put("imgurl",courseEntity.getImg());
				json.put("title",courseEntity.getTitle());
				json.put("time",sdf.format(courseEntity.getClasstime()));
				JSONArray array = new JSONArray();
				if(StringUtils.isNotBlank(courseEntity.getClasstimeids())){
					String[] ids = courseEntity.getClasstimeids().split(",");
					for(String id:ids){
						ClasstimeEntity classtimeEntity = classtimeService.getById(Integer.parseInt(id));
						JSONObject jsonObject = new JSONObject();
						jsonObject.put("id",classtimeEntity.getId());
						jsonObject.put("name",classtimeEntity.getName());
						jsonObject.put("addtime",sdf.format(classtimeEntity.getCreatetime()));
						array.add(jsonObject);
					}
				}
				json.put("kc",array);
				return R.ok().put("data",json);
			}else{
				return R.ok("找不到该课程");
			}
		}

	}

	/**
	 * 搜索课程
	 * @keyword 搜索的关键字
	 * @userid 用户id
	 * @return
	 */
	@RequestMapping("/search")
	@ResponseBody
	public R search(@RequestParam("keyword")String keyword,@RequestParam("userid")Integer userid) {
		if(keyword==null){
			keyword = "";
		}
		if(userid==null){
			return R.error("用户id不能为空");
		}else {
			UsersVOEntity user = usersService.getById(userid);
			List<CourseEntity> list = null;
			if(user.getRole()==0){//学生
				list = courseService.searchCourse(keyword, userid);
			}else{//老师
				list = courseService.searchTeacherCourse(keyword, userid);
			}

			if (list != null && list.size() > 0) {
				JSONArray array = new JSONArray();
				for (CourseEntity entity : list) {
					JSONObject json = new JSONObject();

					json.put("id", entity.getId());
					json.put("imgurl", entity.getImg());

					array.add(json);

				}
				return R.ok().put("data", array);

			} else {
				return R.ok("没有数据");
			}
		}
	}

	/**
	 * 课程信息
	 * @courseid 课程id
	 * @userid 用户id
	 * @return
	 */
	@RequestMapping("/getCourseInfo")
	@ResponseBody
	public R getCourseInfo(@RequestParam("courseid")Integer courseid,@RequestParam("userid")Integer userid) {
		if(courseid==null){
			return R.error("课程id不能为空");
		}else if(userid==null){
			return R.error("用户id不能为空");
		}else {
			UsersVOEntity user = usersService.getById(userid);
			if(user!=null){
				CourseEntity entity = courseService.getById(courseid);
				if(entity !=null){
					JSONObject json = new JSONObject();

					json.put("id", entity.getId());
					json.put("imgurl", entity.getImg());
					json.put("explain", entity.getExplains());
					json.put("userInfo",StringUtils.isNotBlank(user.getTelphone())?true:false);
					return R.ok().put("data",json);
				}else{
					return R.ok("找不到该课程信息");
				}
			}else{
				return R.ok("找不到该学生信息");
			}

		}
	}

    /**
     * 课程信息
     * @courseid 课程id
     * @userid 用户id
     * @return
     */
    @RequestMapping("/getCourseList")
    @ResponseBody
    public R getCourseList(@RequestParam("userid")Integer userid) {
        if(userid==null){
            return R.error("用户id不能为空");
        }else {
            List<CourseEntity> list = courseService.getCourseList(userid);
			JSONArray array = new JSONArray();
            if(list!=null&&list.size()>0){

                for(CourseEntity entity:list){
                    JSONObject json = new JSONObject();

                    json.put("id", entity.getId());
                    json.put("title", entity.getTitle());
                    json.put("imgurl", entity.getImg());
                    json.put("time",sdf.format(entity.getClasstime()));
                    array.add(json);
                }
            }
			return R.ok().put("data",array);

        }
    }

	/**
	 * 学生报名课程
	 * @realname 真实姓名
	 * @telphone 手机号码
	 * @courseid 课程
	 * @userid 用户id
	 * @return
	 */
	@RequestMapping("/enterCourse")
	@ResponseBody
	public R enterCourse(@RequestParam("realname")String realname,@RequestParam("telphone")String telphone,@RequestParam("courseid")Integer courseid,@RequestParam("userid")Integer userid) {
		if(userid==null){
			return R.error("用户id不能为空");
		}else if(courseid == null){
			return R.error("课程id不能为空");
		}else{
			if(StringUtils.isNotBlank(telphone)&&StringUtils.isNotBlank(realname)){
				UsersVOEntity user = usersService.getById(userid);
				if(user !=null){
					user.setTelphone(telphone);
					user.setRealname(realname);
					usersService.saveOrUpdate(user);
				}else{
					return R.ok("该用户不存在");
				}
			}
			courseuserService.addCourse(userid,courseid, Timestamp.valueOf(sdf.format(new Date())));
			return R.ok();
		}
	}

	/**
	 * 课程说明
	 * @param userid 用户id
	 * @return
	 */
	@RequestMapping("/courseDescript")
	@ResponseBody
	public R courseDescript(@RequestParam("userid")Integer userid) {
		if(userid == null){
			return R.error("用户id不能为空");
		}else{
			List<CoursedescriptEntity> list = coursedescriptService.getCourseDescript();
			JSONObject json = new JSONObject();
			if(list !=null && list.size()>0){
				json.put("desc",list.get(0).getDescript());
			}

			JSONArray array = new JSONArray();
			List<CourseEntity> courseEntities = courseuserService.getCanAddCourse(userid);
			if(courseEntities!=null && courseEntities.size()>0){
				for(CourseEntity entity:courseEntities){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("id",entity.getId());
					jsonObject.put("title",entity.getTitle());
					array.add(jsonObject);
				}
				json.put("course",array);
			}
			return R.ok().put("data",json);
		}
	}


	/**
	 * 使用帮助
	 * @return
	 */
	@RequestMapping("/useHelp")
	@ResponseBody
	public R useHelp() {

		List<UsehelpEntity> list = usehelpService.getUseHelp();

		if(list !=null && list.size()>0){
			UsehelpEntity entity = list.get(0);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("title",entity.getContext());
			return R.ok().put("data",jsonObject);
		}else{
			return R.ok("没有数据");
		}
	}

	/**
	 * 学生答题列表
	 * @param courseid 课程id
	 * @param classtimeid 课次id
	 * @return
	 */
	@RequestMapping("/studentAnswer")
	@ResponseBody
	public R studentAnswer(@RequestParam("courseid")Integer courseid,@RequestParam("classtimeid")Integer classtimeid) {

		if(courseid == null){
			return R.error("课程id不能为空");
		}else if(classtimeid ==null){
			return R.error("课次id不能为空");
		}else{
			List<CourseuserVOEntity> list = courseuserService.getStudentAnswerList(courseid,classtimeid);

			if(list !=null && list.size()>0){
				JSONArray array = new JSONArray();
				for(CourseuserVOEntity entity:list){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("userid",entity.getUserid());
					jsonObject.put("courseid",entity.getCourseid());
					jsonObject.put("name",entity.getStudentname());
					jsonObject.put("time",sdf.format(entity.getAnswertime()));
					array.add(jsonObject);
				}
				return R.ok().put("data",array);
			}else{
				return R.ok("没有数据");
			}
		}

	}

	/**
	 * 上传答案
	 * @return
	 */
	@RequestMapping(value="/uploadAnswer", method=RequestMethod.POST)
	@ResponseBody
	public R uploadAnswer(@RequestBody String param) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		JSONObject json = JSONObject.fromObject(param);

		String courseidstr = json.getString("courseid");

		String classtimeidstr = json.getString("classtimeid");

		String useridstr = json.getString("userid");

		String answer = json.getString("answer");
System.out.println("==========="+courseidstr+"=============="+useridstr+"================="+answer);
		if(StringUtils.isBlank(courseidstr)){
			return R.error("课程id不能为空");
		}else if(StringUtils.isBlank(classtimeidstr)){
			return R.error("课次id不能为空");
		}else if(StringUtils.isBlank(useridstr)){
			return R.error("用户id不能为空");
		}else if(StringUtils.isBlank(answer)){
			return R.error("答案不能为空");
		}else{
			FileWriter writer;
			File file = new File(this.getClass().getResource("/").getPath()+"\\img");
			String url = file.toString();
			url = url+"\\"+format.format(new Date())+".txt";
			try {
				if(!file.exists()){
					file.mkdirs();
				}
				writer = new FileWriter(url);
				writer.write(answer);
				writer.flush();
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

			Integer courseid = Integer.parseInt(courseidstr);
			Integer classtimeid = Integer.parseInt(classtimeidstr);
			Integer userid = Integer.parseInt(useridstr);
			UsersVOEntity user = usersService.getById(userid);
			if(user !=null){
				if(user.getRole()==0){//学生
					ClasstimeuserEntity classtimeuserEntity = new ClasstimeuserEntity();
					classtimeuserEntity.setAnswertime(new Date());
					classtimeuserEntity.setClasstimeid(classtimeid);
					classtimeuserEntity.setCourseid(courseid);
					classtimeuserEntity.setUserid(userid);
					classtimeuserEntity.setTopicanalysis(url);
					classtimeuserService.save(classtimeuserEntity);
				}else{//老师
					ClasstimeteacherEntity classtimeteacherEntity = new ClasstimeteacherEntity();
					classtimeteacherEntity.setAnswertime(new Date());
					classtimeteacherEntity.setClasstimeid(classtimeid);
					classtimeteacherEntity.setCourseid(courseid);
					classtimeteacherEntity.setUserid(userid);
					classtimeteacherEntity.setTopicanalysis(url);
					classtimeteacherService.save(classtimeteacherEntity);

				}
				return R.ok();
			}else{
				return R.ok("没有该用户");
			}
		}

	}


	/**
	 * 获取问题
	 * @param courseid 课程id
	 * @param classtimeid 课次id
	 * @param userid 用户id
	 * @return
	 */
	@RequestMapping("/getQuestion")
	@ResponseBody
	public R getQuestion(@RequestParam("courseid")Integer courseid,@RequestParam("userid")Integer userid,@RequestParam("classtimeid")Integer classtimeid) {

		if(courseid == null){
			return R.error("课程id不能为空");
		}else if(classtimeid == null){
			return R.error("课次id不能为空");
		}else if(userid == null){
			return R.error("用户id不能为空");
		}else{
			UsersVOEntity user = usersService.getById(userid);
			if(user !=null){
				ClasstimeEntity entity = classtimeService.getById(classtimeid);
				if(entity!=null){
					JSONArray array = new JSONArray();
					if(StringUtils.isNotBlank(entity.getQuestionids())){
						String[] idArray = entity.getQuestionids().split(",");

						for(String id:idArray){
							JSONObject json = new JSONObject();
							QuestionEntity questionEntity = questionService.getById(Integer.parseInt(id));
							json.put("id",questionEntity.getId());
							json.put("name",questionEntity.getNameimg());

							array.add(json);
						}
					}

					ClasstimeuserEntity classtimeuserEntity = classtimeuserService.getStudentAnswer(courseid,userid,classtimeid);//学生答题
					ClasstimeteacherEntity classtimeteacherEntity = classtimeteacherService.getTeacherAnswer(courseid,classtimeid);//老师答案

					if(user.getRole() ==0){//学生
						return R.ok().put("role",user.getRole()).put("question",array).put("coursename",entity.getName())
								.put("teacheranswer",classtimeteacherEntity!=null?true:false)
								.put("studentanswer",classtimeuserEntity!=null?true:false)
								.put("vediourl",classtimeuserEntity!=null?classtimeuserEntity.getVediourl():"");
					}else{//老师
						return R.ok().put("role",user.getRole()).put("question",array).put("coursename",entity.getName())
								.put("teacheranswer",classtimeteacherEntity!=null?true:false)
								.put("vediourl",classtimeteacherEntity!=null?classtimeteacherEntity.getVediourl():"");

					}

				}else{
					return R.ok("没有该课次");
				}

			}else{
				return R.ok("没有该用户");
			}
		}
	}

	/**
	 * 获取学生答案
	 * @return
	 */
	@RequestMapping(value="/getStudentAnswer", method=RequestMethod.POST)
	@ResponseBody
	public JSONObject getStudentAnswer(@RequestBody String param) {

		JSONObject jsonObject = new JSONObject();
		JSONObject json = JSONObject.fromObject(param);

		String courseidstr = json.getString("courseid");

		String classtimeidstr = json.getString("classtimeid");

		String useridstr = json.getString("userid");

		if(StringUtils.isBlank(courseidstr)){
			jsonObject.put("msg","课程id不能为空");
			return jsonObject;
		}else if(StringUtils.isBlank(classtimeidstr)){
			jsonObject.put("msg","课次id不能为空");
			return jsonObject;
		}else if(StringUtils.isBlank(useridstr)){
			jsonObject.put("msg","用户id不能为空");
			return jsonObject;
		}else{

			Integer courseid = Integer.parseInt(courseidstr);
			Integer classtimeid = Integer.parseInt(classtimeidstr);
			Integer userid = Integer.parseInt(useridstr);
			UsersVOEntity user = usersService.getById(userid);
			if(user !=null){
				ClasstimeuserEntity classtimeuserEntity = classtimeuserService.getStudentAnswer(courseid,userid,classtimeid);//学生答题

				if(classtimeuserEntity!=null){
					String line = "";
					BufferedReader br = null;
					int num =0;
					try {
						br = new BufferedReader(new FileReader(classtimeuserEntity.getTopicanalysis()));
						line = br.readLine();
						while ((num = br.read())!=-1){
							System.out.println(line);
							line = br.readLine();
						}
					}catch (Exception e){
							e.printStackTrace();
					}finally{
						try{
							if(br != null){
								br.close();
							}

						}catch(IOException ioe){
							System.out.println("关闭InputStream句柄错误: " + ioe);
						}

					}

					jsonObject.put("name",user.getRealname());
					jsonObject.put("time",sdf.format(classtimeuserEntity.getAnswertime()));
					jsonObject.put("answer",line);
					return jsonObject;
				}else{
					jsonObject.put("msg","没有找到该数据");
					return jsonObject;
				}

			}else{
				jsonObject.put("msg","没有该学生信息");
				return jsonObject;
			}

		}
	}

	/**
	 * 获取老师答案
	 * @return
	 */
	@RequestMapping( value="/getTeacherAnswer", method=RequestMethod.POST)
	@ResponseBody
	public JSONObject getTeacherAnswer(@RequestBody String param) {
		JSONObject jsonObject = new JSONObject();
		JSONObject json = JSONObject.fromObject(param);

		String courseidstr = json.getString("courseid");

		String classtimeidstr = json.getString("classtimeid");

		if(StringUtils.isBlank(courseidstr)){
			jsonObject.put("msg","课程id不能为空");
			return jsonObject;
		}else if(StringUtils.isBlank(classtimeidstr)){
			jsonObject.put("msg","课次id不能为空");
			return jsonObject;
		}else{
			Integer courseid = Integer.parseInt(courseidstr);
			Integer classtimeid = Integer.parseInt(classtimeidstr);
			ClasstimeteacherEntity classtimeteacherEntity = classtimeteacherService.getTeacherAnswer(courseid,classtimeid);//老师答案
			if(classtimeteacherEntity !=null){
				UsersVOEntity usersVOEntity = usersService.getById(classtimeteacherEntity.getUserid());
				if(usersVOEntity !=null){
					String line = "";
					BufferedReader br = null;
					int num =0;
					try {
						br = new BufferedReader(new FileReader(classtimeteacherEntity.getTopicanalysis()));
						line = br.readLine();
						while ((num = br.read())!=-1){
							line = br.readLine();
						}
					}catch (Exception e){
						e.printStackTrace();
					}finally{
						try{
							if(br != null){
								br.close();
							}

						}catch(IOException ioe){
							System.out.println("关闭InputStream句柄错误: " + ioe);
						}

					}

					jsonObject.put("name",usersVOEntity.getRealname());
					jsonObject.put("time",sdf.format(classtimeteacherEntity.getAnswertime()));
					jsonObject.put("answer",line);
					return jsonObject;
				}else{
					jsonObject.put("msg","无法获取老师信息");
					return jsonObject;
				}

			}else{
				jsonObject.put("msg","无法获取该课次解析");
				return jsonObject;
			}

		}
	}
}
